package com.icow.wms.basic.controller;

import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.core.page.TableDataInfo;
import com.icow.common.enums.BusinessType;
import com.icow.common.utils.StringUtils;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.wcs.dao.ShelfDao;
import com.icow.wcs.domain.Box;
import com.icow.wcs.domain.ShelfDomain;
import com.icow.wms.basic.domain.ProductDomain;
import com.icow.wms.basic.domain.WvStoreInfo;
import com.icow.wms.basic.service.IProductDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存统计Controller
 * 
 * @author lqq
 * @date 2021-02-02
 */
@RestController
@RequestMapping(value = {"/wms.basic/wvStoreInfo", "/api/wvStoreInfo"})
public class WvStoreInfoController extends BaseController
{
    @Autowired
    private IProductDomainService productDomainService;

    @Autowired
    private ShelfDao shelfDao;
    /**
     * 查询
     */
//    @PreAuthorize("@ss.hasPermi('wms.basic:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(WvStoreInfo wvStoreInfo)
    {
        startPage();
        List<WvStoreInfo> list = getStoreInfo(wvStoreInfo.getGoodsCode());
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
//    @PreAuthorize("@ss.hasPermi('wms.basic:product:export')")
    @Log(title = "wvStoreInfo", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WvStoreInfo wvStoreInfo)
    {
        List<WvStoreInfo> list = getStoreInfo(wvStoreInfo.getGoodsCode());
        ExcelUtil<WvStoreInfo> util = new ExcelUtil<WvStoreInfo>(WvStoreInfo.class);
        return util.exportExcel(list, "wvStoreInfo");
    }


    //获取托盘上物料总数量
    private List<WvStoreInfo> getStoreInfo(String goodsIds)
    {
        List<WvStoreInfo> wvStoreInfoList=new ArrayList<>();
        Map<String, Double> result=new HashMap<>();
        List<Box> temp=new ArrayList<>();
        if(StringUtils.isNotEmpty(goodsIds)) {
            List<ShelfDomain> list=shelfDao.findAll()
                    .stream()
                    .filter(o->o.getBoxes()!=null&&o.getBoxes().size()>0&&o.getBoxes().stream().filter(d->d!=null&&d.getProductNo()!=null&& d.getProductCount() != null&&goodsIds.contains(d.getProductNo())).count()>0)
                    .collect(Collectors.toList());

            for (ShelfDomain item : list
            ) {
                List<Box> boxList = item.getBoxes().stream().filter(d -> d != null && d.getProductNo() != null&& d.getProductCount() != null && goodsIds.contains(d.getProductNo())).collect(Collectors.toList());
                temp.addAll(boxList);
            }
        }
        else
        {
            List<ShelfDomain> list=shelfDao.findAll()
                    .stream()
                    .filter(o->o.getBoxes()!=null&&o.getBoxes().size()>0&&o.getBoxes().stream().filter(d->d!=null&&d.getProductNo()!=null&& d.getProductCount() != null).count()>0)
                    .collect(Collectors.toList());
            for (ShelfDomain item : list
            ) {
                List<Box> boxList = item.getBoxes().stream().filter(d -> d != null && d.getProductNo() != null&& d.getProductCount() != null).collect(Collectors.toList());
                temp.addAll(boxList);
            }
        }

        result=temp.stream().collect(Collectors.groupingBy(Box::getProductNo, Collectors.summingDouble(Box::getProductCount)));

        for(String key:result.keySet())
        {
            WvStoreInfo item=new WvStoreInfo();
            ProductDomain p=new ProductDomain();
            p.setCode(key);
            List<ProductDomain> plist=productDomainService.selectProductDomainList(p);
            String goodsName=plist.size()>0?plist.get(0).getName():"";
            item.setGoodsName(goodsName);
            item.setGoodsCode(key);
            item.setGoodsQua(result.get(key).toString());
            wvStoreInfoList.add(item);
        }
        return  wvStoreInfoList;
    }
}
